<?
  class dv_model extends add_model_module{
   function get_doc_title($id){    $tmp=$this->db->row_array($this->db->query("SELECT id_lang_text.text From id_lang_text,ns_doc,doc_view WHERE ns_doc.id='$id' and ns_doc.id=doc_view.id and doc_view.show_me_title='1' and doc_view.text_title=id_lang_text.id and  id_lang_text.lang='".$_SESSION['lang']."'"));
    return $tmp;
   }

   function get_doc($id){
   	$tmp= $this->db->result_array($this->db->query("SELECT ns_doc.id as idg,ns_doc.side_for_doc,id_lang_text.text,title.text as title,doc_view.small_version,title.show_me_title,id_lang_text.id From id_lang_text,doc_view,ns_doc inner join (SELECT doc_view.show_me_title,id_lang_text.text,doc_view.id From id_lang_text,doc_view WHERE doc_view.text_title=id_lang_text.id AND id_lang_text.lang='".$_SESSION['lang']."') as title ON title.id=ns_doc.id  WHERE ns_doc.father_id='$id' and ns_doc.id=doc_view.id and doc_view.text=id_lang_text.id and ns_doc.show_me=1 and id_lang_text.lang='".$_SESSION['lang']."'"));

  	return $tmp;
   }

   function get_text($id){   	return  $this->db->row_array($this->db->query("SELECT modules.*,ns_doc.side_for_doc,id_lang_text.text,ns_doc.id,id_lang_text.lang FROM modules,id_lang_text,doc_view,ns_doc WHERE modules.class=ns_doc.module AND ns_doc.id=".$id." AND ns_doc.show_me=1 AND ns_doc.id=doc_view.id AND  doc_view.text=id_lang_text.id AND id_lang_text.lang='".$_SESSION['lang']."';"));
   }

      function get_text2($id,$lang){
   	return  $this->db->row_array($this->db->query("SELECT ns_doc.show_me,modules.*,ns_doc.side_for_doc,id_lang_text.text,ns_doc.id,id_lang_text.lang FROM modules,id_lang_text,doc_view,ns_doc WHERE modules.class=ns_doc.module AND ns_doc.id=".$id."  AND ns_doc.id=doc_view.id AND  doc_view.text=id_lang_text.id AND id_lang_text.lang='".$lang."';"));
   }

   function insert($func,$id,$module,$lang,$lastid){
    $lastid++;
    $lastid2=$lastid+1;   	foreach($lang as $key=>$value){   	$this->db->query("INSERT INTO id_lang_text SET id=".$lastid.",text='default".$value['alias']."',lang='".$value['name']."'; ");
   	$this->db->query("INSERT INTO id_lang_text SET id=".$lastid2.",text='default".$value['alias']."',lang='".$value['name']."'; ");
   	}    $this->db->query("INSERT INTO ns_doc SET father_id=".$id.",show_me=1,side_for_doc='body',module='".$module."';");
    $ns_docL=mysql_insert_id();
    $this->db->query("INSERT INTO doc_view SET id=".$ns_docL.",text_title=".$lastid.",text=".$lastid2.",show_me_title=1,small_version=0;");
    return $ns_docL;
   }

   function returnSide(){   	return $this->db->result_array($this->db->query("SELECT * From side_site WHERE id_template=( SELECT id From templates WHERE show_me='1')"));
   }

   function update($id){    $this->db->query("UPDATE ns_doc SET side_for_doc='".$_POST['sidedoc']."' WHERE id=".$id.";");
    $this->db->query("UPDATE doc_view SET small_version='".$_POST['small_version']."',show_me_title='".$_POST['show_me_title']."' WHERE id=".$id.";");
    $result=$this->db->row_array($this->db->query("SELECT * FROM doc_view WHERE id=".$id.";"));
    foreach($_POST['title'] as $key=>$value){    $this->db->query("UPDATE id_lang_text SET text='".$value."' WHERE id=".$result['text_title']." AND lang='".$key."'");
    //$this->db->query("UPDATE id_lang_text SET text_title='".$value."' WHERE id=".$result['texttitle']." AND lang='".$key."'");
    }
      foreach($_POST['text'] as $key=>$value){
    $this->db->query("UPDATE id_lang_text SET text='".$value."' WHERE id=".$result['text']." AND lang='".$key."'");
    }

   }

   function getDoc($id,$lang){    return $this->db->row_array($this->db->query("SELECT ns_doc.id as idg,ns_doc.side_for_doc,lang.*,id_lang_text.text,title.text as title,doc_view.small_version,title.show_me_title,id_lang_text.id From lang,id_lang_text,doc_view,ns_doc inner join (SELECT doc_view.show_me_title,id_lang_text.text,doc_view.id From id_lang_text,doc_view WHERE doc_view.text_title=id_lang_text.id AND id_lang_text.lang='".$lang."') as title ON title.id=ns_doc.id  WHERE ns_doc.id='$id' and ns_doc.id=doc_view.id and doc_view.text=id_lang_text.id and ns_doc.show_me=1 and id_lang_text.lang='".$lang."' AND lang.name=id_lang_text.lang"));
   }




   function del($id,$lang){   	if(!empty($lang))
   		 return $this->db->query("DELETE id_lang_text FROM id_lang_text,doc_view,ns_doc WHERE (doc_view.text=id_lang_text.id OR doc_view.text_title=id_lang_text.id) AND doc_view.id=".$id." AND id_lang_text.lang='".$lang."' AND ns_doc.id=doc_view.id;");
   		 else
   		 return $this->db->query("DELETE id_lang_text,doc_view,ns_doc FROM id_lang_text,doc_view,ns_doc WHERE (doc_view.text=id_lang_text.id OR doc_view.text_title=id_lang_text.id) AND doc_view.id=".$id."  AND ns_doc.id=doc_view.id;");
   		}




  }



?>